What is strip-indent?
The strip-indent npm package is used to remove the leading whitespace from every line in a given text block. This is particularly useful when working with multi-line strings that need to be displayed without indentation or when processing template literals in JavaScript that should not retain the formatting of the source code.
What are strip-indent's main functionalities?
Strip indentation from a multi-line string
This feature allows you to remove the common leading whitespace from all lines in a text block, making it easier to handle formatted multi-line strings without unwanted indentation.
const stripIndent = require('strip-indent');
const text = ` line one
line two
line three`;
console.log(stripIndent(text));
Other packages similar to strip-indent
dedent
Dedent is similar to strip-indent but also trims the trailing newline. It is often used in template literals to avoid first-line indentation and to ensure that the output does not end with a newline, making it slightly different in its approach compared to strip-indent.
indent-string
While strip-indent removes indentation, indent-string adds indentation to each line in a string. This package can be considered the opposite of strip-indent. It allows specifying the number of spaces or the type of indentation (spaces or tabs), offering more flexibility depending on the user's needs.
strip-indent
Strip leading whitespace from each line in a string
The line with the least number of leading whitespace, ignoring empty lines, determines the number to remove.
Useful for removing redundant indentation.
Install
$ npm install strip-indent
Usage
import stripIndent from 'strip-indent';
const string = '\tunicorn\n\t\tcake';
stripIndent(string);
Related